class AuditorController < ApplicationController
  before_filter :user_authorize
  before_filter :sys_before_log
  before_filter :privilege_filter,:except=>[:auditorList,:password,:newAuditor,:editAuditor,:passwordForAuditor]

  def auditorList

  end

  def auditorListForAuditor

  end

  def editAuditor

  end

  def editAuditorInformation
    
  end

  def newAuditor

  end

  def password

  end

  def passwordForAuditor
    
  end
  #得到审批人列表
  def getAuditors
    pageBegin =  params[:start]
    pageEnd =  params[:limit]
    logger.info session[:auditor_type].to_s+"============================"
    #auditors = Auditor.findAuditors(params[:auditor_number].to_s,params[:auditor_name].to_s,params[:state].to_s,session[:auditor_type],pageBegin,pageEnd)
    #2011-08-15,jerry,这儿不论审批人是设备科还是其它部门，都一次性抓取数据。
    #这儿的auditor_number，其实是登录的login_id :(
    auditors = Auditor.where("login_id like ? AND name like ? AND auditing_status like ?",'%'+params[:auditor_number].to_s+'%','%'+params[:auditor_name].to_s+'%','%'+params[:state].to_s+'%').limit(params[:limit].to_i).offset(params[:start].to_i)
    jsonstr="{totalProperty:"+auditors.length.to_s+",root:"+auditors.to_json(:include => [:department,:role])+"}"
    #logger.info jsonstr
    render :text=>jsonstr, :layout=>false
  end

  #审批人得到自己的信息
  def getMyInformation
    auditor = Auditor.find(session[:auditor_id])
    jsonstr="{totalProperty:1,root:"+auditor.to_json(:include => [:department])+"}"
    #logger.info jsonstr
    render :text=>jsonstr, :layout=>false
  end

  #启用审批人
  def applyPass
    begin
      ids= params[:id][1..params[:id].length-2].split(',');
      for id in ids
        auditor = Auditor.find(id)
        auditor.auditing_status = '启用'
        auditor.save
      end
      info = 'success'
    rescue Exception=>e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #禁用审批人
  def stopAuditor
    begin
      ids= params[:id][1..params[:id].length-2].split(',');
      for id in ids
        auditor = Auditor.find(id)
        auditor.auditing_status = '禁用'
        auditor.save
      end
      info = 'success'
    rescue Exception=>e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #添加审批人信息
  def create
    begin
      auditor = Auditor.findByNumber(params[:login_id])
      if auditor==nil
        auditor = Auditor.new
        auditor.login_id = params[:login_id]
        auditor.name = params[:name]
        auditor.phone = params[:phone]
        auditor.mobile = params[:mobile]
        auditor.unit_price_ceiling = params[:unit_price_ceiling]
        auditor.amount_ceiling = params[:amount_ceiling]
        auditor.email = params[:email]
        auditor.department_id = params[:department_id]
        auditor.role_id = params[:role_id]
        auditor.auditor_type = params[:auditor_type]
        auditor.password = params[:login_id]
        auditor.is_admin = params[:is_admin]
        auditor.is_chief = params[:is_chief]
        auditor.auditing_status = '启用'

        #设备科的超级审批人只能添加设备科的审批人，房产办的超级审批人只能添加房产办的审批人
        auditor.auditor_type = session[:auditor_type]
        auditor.save!
        info = 'success'
      else
        info = '该编号已被使用，请更换编号'
      end
    rescue Exception => e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #获得指定ID的审批人信息
  def edit
    if session[:is_admin]!=nil && session[:is_admin]==1
      auditor = Auditor.find(params[:id])
    elsif session[:is_admin]!=nil && session[:is_admin]!=1
      auditor = Auditor.find(session[:auditor_id])
    end
    render :text => auditor.to_json(),:layout => false
  end

  #更新审批人信息
  def update
    begin
      auditor = Auditor.findByNumber(params[:login_id]) #建议修改成find_by_login_id
      if Auditor.find(params[:id]).login_id != params[:login_id] && auditor != nil
        info = '该编号已被使用，请更换编号'
      else
        auditor = Auditor.find(params[:id])
        auditor.login_id = params[:login_id]
        auditor.name = params[:name]
        auditor.email = params[:email]
        auditor.phone = params[:phone]
        auditor.mobile = params[:mobile]
        auditor.unit_price_ceiling = params[:unit_price_ceiling]
        auditor.amount_ceiling = params[:amount_ceiling]
        auditor.department_id = params[:department_id]
        auditor.role_id = params[:role_id]
        auditor.auditor_type = params[:auditor_type]
        auditor.is_admin = params[:is_admin]
        auditor.is_chief = params[:is_chief]
        auditor.save!
        info = 'success'
      end
    rescue Exception => e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #审批人修改自己信息
  def updateInformation
    begin
      auditor = Auditor.find(session[:auditor_id])
      auditor.name = params[:name]
      auditor.email = params[:email]
      auditor.phone = params[:phone]
      auditor.mobile = params[:mobile]
      auditor.save!
      info = 'success'
    rescue Exception => e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #审批人修改自己的密码
  def changePassword
    begin
      auditor = Auditor.find(session[:auditor_id])
      if auditor.password == params[:password]
        auditor.password = params[:password1]
        auditor.save!
        info = 'success'
      else
        info = '原密码错误，请确认'
      end
    rescue Exception => e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #审批人修改密码
  def changePasswordForAuditor
    begin
      auditor = Auditor.find(params[:id])
      auditor.password = params[:password1]
      auditor.save!
      info = 'success'
    rescue Exception => e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end
  
end
